"""
    Random Variate Comparison
    Compares random variate functions in python and graphs results

    Functions
    random.gauss
    random.lognormvariate
    random.normalvariate
"""
#
# IMPORTS
#
import random
import pylab
import matplotlib.pyplot as pyplot


#
# CONSTANTS
#
MU = 100
SIGMA = 25
DATASET_SIZE = 1000


#
# FUNCTIONS
#
def collect_dataset(method, mu, sigma):
    dataset = []
    for n in range(DATASET_SIZE):
        dataset.append(method(mu, sigma))
    return dataset


def visualize_results(datasets):

    fig = pyplot.figure()
    ax = None

    for (color,label),data in datasets.items():

        if not ax:
            ax = fig.add_subplot(111)
        else:
            ax = ax.twinx()

        ax.set_ylabel(label, color=color)

        # the histogram of the data
        n, bins, patches = ax.hist(data, 50, normed=1, facecolor=color, alpha=0.6)



    ax.grid(True)
    pyplot.show()
    return




    ax = fig.add_subplot(2,1,1)
    color = 100000

    for variate,data in datasets.items():
        color += color
        hexcolor = '#%06x' % (color)
        line, = ax.plot(data, color=hexcolor, lw=2)
        #print variate, random.sample(datasets[variate], 10)

    pylab.show()


def main():
    datasets = {}

    methods = [
        (random.normalvariate, MU, SIGMA, 'red', 'normal'),
        (random.gauss, MU, SIGMA, 'yellow', 'gauss'),
        (random.lognormvariate, 4.6, 0.1, 'blue', 'log normal'),
    ]

    for method,mu,sigma,color,label in methods:
        datasets[(color,label)] = collect_dataset(method, mu, sigma)

    visualize_results(datasets)


#
# MAIN
#
if __name__ == '__main__':
    main()
    print 'ok:', __file__

